﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Game_Engine
{
    public class Camera : Component
    {
        public Vector2 Position { get; set; }
        public Vector2 Size { get; set; }
        public Viewport Viewport
        {
            get
            {
                return new Viewport((int)(ScreenManager.Width * Position.X), (int)(ScreenManager.Height * Position.Y),
                                    (int)(ScreenManager.Width * Size.X), (int)(ScreenManager.Height * Size.Y));
            }
        }
        public float FieldOfView
        {
            get;
            set;
        }

        public float AspectRatio
        {
            get;
            set;
        }

        public float NearPlane
        {
            get;
            set;
        }

        public float FarPlane
        {
            get;
            set;
        }

        public Camera()
        {
            FieldOfView = MathHelper.PiOver2;
            AspectRatio = 1;
            NearPlane = 0.1f;
            FarPlane = 100f;
            this.Transform = null;
            Position = Vector2.Zero;
            Size = Vector2.One;
            Name = "Camera";
        }

        public Matrix Projection
        {
            get
            {
                return Matrix.CreatePerspectiveFieldOfView(FieldOfView, AspectRatio, NearPlane, FarPlane);
            }
            set{}
        }

        public Matrix View
        {
            get
            {
                if (Transform == null)
                    return Matrix.Identity;
                return Matrix.CreateLookAt(Transform.Position, Transform.Position + Transform.Forward, Transform.Up);
            }
            set{}
        }

        public Ray ScreenPointToWorldRay(Vector2 position)  //Mouse input feedback will be placed into this parameter
        {
            Vector3 start = Viewport.Unproject(new Vector3(position, 0), Projection, View, Matrix.Identity);    //A point on the near plane corresponding to input
            Vector3 end = Viewport.Unproject(new Vector3(position, 1), Projection, View, Matrix.Identity);      //A point on the far plane corresponding to input

            return new Ray(start, Vector3.Normalize(end - start));
        }
    }
}
